package controllers;

import play.db.jpa.GenericModel;
import play.mvc.*;

import java.util.*;

import models.*;

@With(Secure.class)					//this class is used for the login, Doaa El-Hawary
public class Admins extends Controller {

    public static void index() {
        render();
    }   

    /**
     * Enables an admin to create a new user in the database
     * 
     * @author		Rania Nader Saeed
	 * @version		2011.0428
	 * @category	C1S1 
     * @param 		fname: first name of the new user
     * @param 		lname: last name of the new user
     * @param 		username : username of the new user
     * @param 		password : password of the new user
     * @param		phonenumber : phone number of the new user
     * @param 		address : address of the new user
     * @param 		email : email of the new user
     * @return		boolean True if user is created successfully 
     * 				and false otherwise 
     */
   public static boolean createUser(String fname,
		   							String lname,
		   							String username,
		   							String password,
		   							String phonenumber,
		   							String address,
		   							String email)
   {
	   LinkedList<String> attributes = new LinkedList<String>();
	   attributes.add(fname);
	   attributes.add(lname);
	   attributes.add(username);
	   attributes.add(password);
	   attributes.add(phonenumber);
	   attributes.add(address);
	   attributes.add(email);
	   List<User> user = GenericModel.findAll();
	   User newUser = new User(attributes);
	   
		if (user != null)	   
		{
		   if(user.contains(newUser))
		   {
			   return false;
		   }
		   else
		   {
			  newUser.save();
			  return true;
		   }
	   }
	return false;
   }
	
    

	/**
	 * the method that render the userId to the view
	 *
	 * @author     Doaa El-Hawary
	 * @version    2011.0505
	 * @category   C1S2
	 * @params     Attributes: the attributes of the given user that should be edited,
	 * @params	   UserId: the user whose info should be edited.
	 * @return		void, it just edits the info and then saves the updated info of the user
	 */
	public static void editUsersInfo(Long userId,
						String fname,
						String lname,
						String username,
						String password,
						String phoneNumber,
						String address,
						String email,
						boolean isAdmin) {
		User user = User.findById(userId);
		/*String firstname = user.getfName();
		String lastname = user.getlName();
		String usernaame = user.getUsername();
		String userPassword = user.getPassword();
		String userPhone = user.getPhoneNumber();
		String userAddress = user.getAddress();
		String userEmail = user.getEmail();
		boolean userAdmin = user.isAdmin();*/
		user.editInfo(fname, lname, username, password, phoneNumber, address, email, isAdmin);
		user.save();
		//render(userId);
	}
	
	/**view of admin
     * @author Tarek Sheasha
     * @param adminId
     */
	public static void viewAdmin(long adminId) {
		Admin admin = Admin.findById(adminId);
		render(admin);
	}
	
	/*public static void viewAllUsers() {
		List<String> allUsersStrings = new ArrayList<String> ();
		List<User> allUsers = User.findAll();
		for(int i=0; i<allUsers.size(); i++) {
			User tempUser = allUsers.get(i);
			String tempString = tempUser.viewUsersInfo();
			allUsersStrings.add(tempString);
		}
		render(allUsersStrings);
	}*/
	
	
	/**
	 * the method that edits the given user's info
	 *
	 * @author     Doaa El-Hawary
	 * @version    2011.0505
	 * @category   C1S2
	 * @params	   UserId: the user whose info should be edited.
	 */
	public static void editHelper(long userId) {
		User user = User.findById(userId);
		String firstname = user.getfName();
		String lastname = user.getlName();
		String usernaame = user.getUsername();
		String userPassword = user.getPassword();
		String userPhone = user.getPhoneNumber();
		String userAddress = user.getAddress();
		String userEmail = user.getEmail();
		boolean userAdmin = user.isAdmin();
		render(userId, firstname, lastname, usernaame, userPassword, userPhone, userAddress, userEmail, userAdmin);
	}
	
}